package com.zdy.im.user.dao;


import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sixeco.framework.common.data.BasePage;
import com.sixeco.framework.core.db.IterableConverter;
import com.zdy.im.user.dto.response.ImJoinFriendResponseDto;
import com.zdy.im.user.entity.ImJoinFriend;
import com.zdy.im.user.mapper.ImJoinFriendMapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;

import com.sixeco.framework.util.PageUtils;
import com.sixeco.framework.core.db.ext.Query;
import com.sixeco.framework.core.mybatis.MyBatisBaseCacheDao;

import java.util.ArrayList;
import java.util.List;




@Service("imJoinFriendDao")
public class ImJoinFriendDao extends MyBatisBaseCacheDao<ImJoinFriendMapper, ImJoinFriend> {

    @Autowired
    private ImJoinFriendMapper  imJoinFriendMapper;

    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ImJoinFriend>  iPage =  new Query<ImJoinFriend>().getPage(params);

        QueryWrapper<ImJoinFriend> query =  new QueryWrapper<>();

        IPage<ImJoinFriend> page = this.page(iPage,query );

        return new PageUtils(page);
    }


    @Override
    public void saveData(List<Object> list) {
        List<ImJoinFriend> datas = new ArrayList<>();
        list.forEach(e -> datas.add(ImJoinFriend.build().clone(e)));
        this.saveBatch(datas, BATCH_SIZE);
        }

    public List<ImJoinFriendResponseDto> getImJoinFriendByUserIdPage(BasePage basePage){


        List<ImJoinFriendResponseDto> resDtoList = new ArrayList<>();
        if(basePage == null){
            return resDtoList;
        }
        //分页对象        PageHelper
        Page<ImJoinFriend> iPage = new Page<>(basePage.getPageNo(), basePage.getRealPageSize());
        iPage.setSearchCount(false);
        QueryWrapper<ImJoinFriend> queryWrapper = new QueryWrapper<>();
        long userId = (long) basePage.getParamObject();
        queryWrapper.eq("to_id", userId);
        queryWrapper.lt("join_status", 3);//小于3, 值:0.未读 1.同意  2.拒绝,3.删除
        //倒序排序
        iPage.addOrder(OrderItem.desc("id"));

        IPage<ImJoinFriend> page = this.page(iPage, queryWrapper);
        if(null == page){
            return resDtoList;
        }

        List<ImJoinFriend>  list = page.getRecords();
        if(null == list || list.isEmpty()){
            return resDtoList;
        }
        resDtoList = IterableConverter.convertList(list,ImJoinFriendResponseDto.class);
        return resDtoList;


    }

    public List<ImJoinFriendResponseDto> getImJoinFriendByPage(BasePage basePage){


        List<ImJoinFriendResponseDto> resDtoList = new ArrayList<>();
        if(basePage == null){
            return resDtoList;
        }
        //分页对象        PageHelper
        IPage<ImJoinFriend> iPage = new Page<>(basePage.getPageNo(), basePage.getRealPageSize());
        QueryWrapper<ImJoinFriend> queryWrapper = new QueryWrapper<>();
        //todo 业务

        IPage<ImJoinFriend> page = this.page(iPage, queryWrapper);
        if(null == page){
            return resDtoList;
        }

        List<ImJoinFriend>  list = page.getRecords();
        if(null == list || list.isEmpty()){
            return resDtoList;
        }
        list.forEach(imJoinFriend -> {
            ImJoinFriendResponseDto imJoinFriendResponseDto = ImJoinFriendResponseDto.build().clone(imJoinFriend);

            resDtoList.add(imJoinFriendResponseDto);
        });
        return resDtoList;


    }

    /** 通过我的id和申请的好友id 查看申请记录 **/
    public ImJoinFriend getImJoinFriendByUserAndFriendId(long userId , long friendId){
        QueryWrapper<ImJoinFriend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("send_user_id", userId);
        queryWrapper.eq("to_id", friendId);
        queryWrapper.orderByDesc("modify_date");

        List<ImJoinFriend>  dbList = this.list(queryWrapper);
        if(null == dbList || dbList.isEmpty()){
            return null;
        }
        return dbList.get(0);
    }

}